import csv
import json
import random
import re
import os
import time

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
# 模拟浏览器
headers["X-Forwarded-For"] = "%s.%s.%s.%s" % (
    random.randrange(1, 200, 20), random.randrange(1, 200, 20), random.randrange(1, 200, 20),
    random.randrange(1, 200, 20))  # IP地址构造

work_dir = "./data/top/"
files = os.listdir(work_dir)

# seriesIds = set()
# for file in files:
#     if file == 'dataAnalacy':
#         continue
#     with open(work_dir + file, 'r', encoding='utf-8') as fs:
#         for f in fs:
#             print(f)

with open('./data/series.txt', 'r', encoding="utf-8") as file:
    resultList = []
    for line in file:
        li = line.split(",")
        seriesId = li[0]
        series = li[1]

        print("bySeries,", seriesId, series)
        url = 'https://price.pcauto.com.cn/price/api/v1/salesCars/getSalesCounter'
        data = {"seriesId": seriesId, "series": series.strip(), "yearAndMonth": [], "salesData": []}

        for year in range(2018, 2022):
            try:
                param = {
                    "qYear": year,
                    "qMonth": 9,
                    "type": 3,
                    "num": 12,
                    "typeId": seriesId.split("sg")[1],
                }
                print(seriesId, year)
                time.sleep(random.randint(1, 3))
                ret = requests.get(url=url, headers=headers, params=param).json()  # 发送请求
                data["yearAndMonth"].append(ret["yearAndMonth"])
                data["salesData"].append(ret["salesData"])
                print(ret)
                # with open("data/saleByTopSeries.json", 'a', encoding='gbk') as f:
                #     f.write(json.dumps(ret)+",")
            except:
                pass
        data["yearAndMonth"] = [x.replace("年", "/").replace("月", "") for j in data["yearAndMonth"] for x in j]
        data["salesData"] = [x for j in data["salesData"] for x in j]
        print(data)
        resultList.append(data)
        print(resultList)
        json_str = json.dumps(resultList)
        with open('./data/saleByTopFromSeries.json', 'w', encoding='utf-8') as ff:
            ff.write(json_str)
